NAME GOTOISP
?PR?GotoIsp?GOTOISP SEGMENT CODE
PUBLIC GotoIsp
RSEG ?PR?GotoIsp?GOTOISP
USING 0
GotoIsp:
T2CON   equ  0C8H;
SCONF   equ  0BFH;
FADDRH  equ  0F4H;
FADDRL  equ  0F5H;
FDATA   equ  0F6H;
FCMD    equ  0F7H;
RAM_ADR_H  equ  030h;
RAM_ADR_L  equ  031h;
END_ADR_H  equ  032h;
END_ADR_L  equ  033h;
CHK_SUM    equ	036h;
CODE_SUM_L equ	037H; 
CODE_SUM_H equ	038H;
CODE_SUM_C equ	039H;
COUNT_L    equ  041h;
COUNT_H    equ  042h;
COUNT_C    equ  043h;

start:        
	mov    	dptr, #00h  	;reset external RAM 
;--------- Enable ISP function -----------------Add ISP Enable Function at 2002/02/25
       	mov	FDATA,#55h
       	mov	FDATA,#0AAh
       	mov	FDATA,#55h
;---------- UART -----------------
        mov    	SCONF,#00001111b;ISP enable of bit2        
      	mov    	tmod,#00100000b	;setup timer1
      	orl    	PCON,#80h  	;SMOD = 1
 	mov	ie, #0		;Disable IE
       	mov	t2con, #0	;Disable Timer 2
       	mov  	th1,#0FFh	;** set BR***
        mov     TL1,#0FFH
 reset:	
       	setb  	tr1			;Start UART
 	mov    	SCON,#01010000b		;inilize UART      
       	call    Init
	CALL	INIT_COUNT
MAIN_LOOP:
  	JB  	RI,UR_LOOP
	JMP 	MAIN_LOOP
UR_LOOP:
  	clr     RI
  	mov     a,SBUF
   	movx    @dptr,a   ;
  	xrl     a,#0aah
   	jz      _chk_end
 	inc     dptr      ;;;
  	jmp     MAIN_LOOP

_chk_end:			;check END symbol(55 AA)
        dec     dpl
        mov     a,dpl
        inc     a
        jnz     _chk_end1
        dec     dph
_chk_end1:
        movx    a,@dptr
        inc     dptr
        inc     dptr
        xrl     a,#55h
        jz   	CheckCom   
	JMP	MAIN_LOOP

CheckCom:       
;----------add 55 aa check function at 2002 by Peter Lai---------------------
        mov	a,dph
	mov	RAM_ADR_H,a	;??????dph???RAM_ADR_H
        mov	a,dpl
        mov	RAM_ADR_L,a	;??????dph???RAM_ADR_L
        
	mov	dptr,#00	;??command????
	movx	a,@dptr
  	xrl	a,#01		;??command?01???program
	jz	CheckLength
	movx	a,@dptr
  	xrl	a,#05		;??command?05???program
	jz	CheckLength
	jmp	execute
;------------- 2002/07/03 ????dptr????end address -------------------	
CheckLength:
	mov	a,RAM_ADR_H
	xrl	a,#2
	jz	execute		;??DPH????2(??512 bytes)
	mov	dph,RAM_ADR_H	;???????????dptr?
	mov	dpl,RAM_ADR_L
	jmp	MAIN_LOOP		
;----------------------------------------------------------------------------
execute:
	MOV	CODE_SUM_C,#0AAh	; Pass BIN
	MOV	CODE_SUM_H,#055h	
	MOV	CODE_SUM_L,#0A5h
        clr     tr1
        mov     dptr,#0000h
        movx    a,@dptr

COM1:	CJNE    A,#1,COM2
        JMP     IISP_Program
COM2:	CJNE    A,#2,COM3
        JMP     IISP_PgeErs
COM3:	CJNE    A,#3,COM4
        JMP     IISP_Protect
COM4:	CJNE    A,#4,COM5
        JMP      IISP_Chenk      
COM5:	CJNE    A,#5,COM6
        JMP      IISP_Program 
COM6:	CJNE    A,#9,COM7
        JMP      IISP_McuReset   
com7:   jmp     _FuncFail



IISP_McuReset:
           ljmp  0000h 

IISP_Program:
        ;execute ISP_Program        
	MOV	CODE_SUM_C,#00		; Clear SUM
	MOV	CODE_SUM_H,#00	
	MOV	CODE_SUM_L,#00
        call    ISP_Program
        ;Verify maximum 512 bytes flash data
        mov     dptr,#0003h
        movx    a,@dptr
        mov     FADDRH,a
        inc     dptr
        movx    a,@dptr
        mov     FADDRL,a
        ;reload end address of buffer index
        mov     a,END_ADR_H
        mov     RAM_ADR_H,a
        mov     a,END_ADR_L
        mov     RAM_ADR_L,a
        jmp     _Verify

IISP_PgeErs:
        ;execute ISP_PgeErs
        mov     dptr,#0001h
        movx    a,@dptr
        mov     FADDRH,a
        inc     dptr
        movx    a,@dptr
        mov     FADDRL,a
        mov     FCMD,#82h
                
        mov     dptr,#0002h	;verify 512 bytes flash date
        movx    a,@dptr
        clr     c
        add     a,#0ffh         ;Str addr + 512 byte
        mov     FADDRL,a
        dec     dpl 
        movx    a,@dptr
        addc    a,#01h
        mov     FADDRH,a
        mov     CHK_SUM,#0ffh   ;compare #ffh
_ISP_PgeErs:
        mov     dph,FADDRH	;call Verify
        mov     dpl,FADDRL
        mov     a,#0h
        movc    a,@a+dptr
        xrl     a,#0ffh		;compare to check sum #ffh
	jnz     _FuncFail
        call    MeetStrAdr
        jc      GO_OK
        call    FlhAdrDec
        jmp     _ISP_PgeErs

IISP_Protect:       
        mov     FCMD,#81h	;execute ISP_Protect
GO_OK:
	jmp     _FuncOk
        
IISP_Chenk:
        mov     dptr,#0002h
        movx    a,@dptr
        clr     c
        add     a,#0ffh         ;Str addr + 512 byte
        mov     FADDRL,a
        dec     dpl
        movx    a,@dptr
        addc    a,#01h
        mov     FADDRH,a
        mov     RAM_ADR_H,#02h
        mov     RAM_ADR_L,#02h
        jmp     Blank_Check

;-------------------------------------- Output SBUF DATA
_FuncFail:
	MOV	CODE_SUM_C,#055h	; Fail BIN
	MOV	CODE_SUM_H,#055h	; Fail BIN
	MOV	CODE_SUM_L,#055h
_FuncOk:
        SETB	tr1	
	MOV	SBUF,CODE_SUM_C
 	CALL	INIT_COUNT
UR_TX1: CALL	START_COUNT
        jnb     TI,UR_TX1
        clr     TI
 	CALL	INIT_COUNT
UR_RX1: CALL	START_COUNT
	JNB	RI,UR_RX1
	CLR	RI
	MOV	SBUF,CODE_SUM_H
 	CALL	INIT_COUNT
UR_TX2: CALL	START_COUNT
        jnb     TI,UR_TX2
        clr     TI
 	CALL	INIT_COUNT
UR_RX2: CALL	START_COUNT
	JNB	RI,UR_RX2
	CLR	RI
	MOV	SBUF,CODE_SUM_L
 	CALL	INIT_COUNT
UR_TX3: CALL	START_COUNT
        jnb     TI,UR_TX3
        clr     TI
	JMP	reset

;--------------------------------------------------------
Blank_Check:
        mov     dph,FADDRH	;call Verify
        mov     dpl,FADDRL
        mov     a,#0h
        movc    a,@a+dptr
        xrl     a,#0ffh		;compare to check sum #ffh
	jnz     GO_FAIL
 
	mov     a,FADDRH	; confirm it is end or not
        orl	a,FADDRL
        jz      _FuncOk

        call    FlhAdrDec
        jmp     Blank_Check

_Verify:
        call    LoadData		;read Data from 512 byte RAM 
        mov     CHK_SUM,a
					;2001.12.21 ADD
	mov	dptr,#0
	movx	a,@dptr			;load 00H data,check command
	xrl	a,#5
	jnz	q1
        call	Verify_128k
	jmp	q2                
q1:
        call    Verify
q2:
        jnc     GO_FAIL        
        call    MeetStrAdr	;check end of start address
        jc      GO_PASS        
        call    RAMAdrDec	;RAM buffer index dec        
        call    FlhAdrDec	;Flash program rom address dec
        jmp     _Verify
GO_FAIL:
	JMP	_FuncFail
GO_PASS:
	JMP	_FuncOk
Verify:
        ;verify ROM code
        clr     c
        mov     dph,FADDRH
        mov     dpl,FADDRL
        mov     a,#0h
        movc    a,@a+dptr
	CALL	ADD_CODE	; add value for check sum
        xrl     a,CHK_SUM
        jnz     _Vrf
        setb    c
_Vrf:				
	ret

Verify_128k:			;2001.12.21 ADD
        mov	SCONF,#00001101B	
        ;verify 128k ROM code
        clr     c
        mov     dph,FADDRH
        mov     dpl,FADDRL
        movx    a,@dptr
	CALL	ADD_CODE	; add value for check sum
        xrl     a,CHK_SUM
        jnz     _Vrf_128k
        setb    c
_Vrf_128k:
        mov	SCONF,#00001111B
        ret
;------------------------------------------
RAMAdrDec:
        mov     r0,#RAM_ADR_L
AdrDec:
        dec     @r0
        mov     a,@r0
        inc     a
        jnz     _AdrDec
        dec     r0
        dec     @r0
_AdrDec:
        ret

FlhAdrDec:
        dec     FADDRL
        mov     a,FADDRL
        inc     a
        jnz     _FlhAdrDec
        dec     FADDRH
_FlhAdrDec:
        ret

Init:
        mov     a,#0h
        mov     RAM_ADR_H,a
        mov     RAM_ADR_L,a
        mov     COUNT_H,a
        mov     COUNT_L,a
        mov     CHK_SUM,a
        mov     dptr,#00h
        ret
decDPTR:        
        dec     dpl
        mov     a,dpl
        inc     a
        jnz     _chk_end2
        dec     dph
_chk_end2:
        ret 

ISP_Program:
        ;maxium 512 bytes data program
        ;caculate data program lenth (len=end_addr-str_addr)
        mov     dptr,#0001h
        movx    a,@dptr
        mov     RAM_ADR_H,a
        inc     dptr
        movx    a,@dptr
        mov     RAM_ADR_L,a
        mov     dptr,#0004h
        movx    a,@dptr
        mov     FADDRL,a
        clr     c
        subb    a,RAM_ADR_L
        mov     RAM_ADR_L,a
        dec     dpl 
        movx    a,@dptr
        mov     FADDRH,a
        subb    a,RAM_ADR_H
        mov     RAM_ADR_H,a
        ;record last data byte RAM address (#05h+len-1)
        mov     a,#05h
        clr     c
        add     a,RAM_ADR_L
        mov     RAM_ADR_L,a
        mov     END_ADR_L,a
        mov     a,#0h
        addc    a,RAM_ADR_H
        mov     RAM_ADR_H,a
        mov     END_ADR_H,a
     ;   push     FADDRH
    ;    push     FADDRL
        mov      FCMD,#82h
       ; pop      FADDRL
        ;pop      FADDRH
_Pgm:   ;load data from RAM buffer to SFR
        call    LoadData		
;------------------------------------------------2001.12.21 ADD
	mov	dptr,#0
	movx	a,@dptr		;load 00H data,cheak command
	xrl	a,#5		;0:code flash 5:data flash
	jnz	n1
        mov     FCMD,#0a0h	;set start programming flag	
	jmp	n2                
n1:
;---------------------------------------------------
        mov     FCMD,#80h	;set start programming flag	 
n2:        
        call    MeetStrAdr	;check end of start address
        jc      _ISP_Program        
        call    RAMAdrDec	;RAM buffer index dec        
        call    FlhAdrDec	;Flash program rom address dec
        jmp     _Pgm
_ISP_Program:
        ret
LoadData:
        mov     dph,RAM_ADR_H
        mov     dpl,RAM_ADR_L
        movx    a,@dptr
        mov     FDATA,a
        ret

MeetStrAdr:
        clr     c
        mov     dptr,#0001h
        movx    a,@dptr
        xrl     a,FADDRH
        jnz     _MeetStrAdr
        inc     dptr
        movx    a,@dptr
        xrl     a,FADDRL
        jnz     _MeetStrAdr
        setb    c
_MeetStrAdr:
        ret

ADD_CODE:
	MOV	R0,A  
	CLR	C
	ADD	A,CODE_SUM_L 
	MOV	CODE_SUM_L,A
	JNC	NO_CARRY
	INC	CODE_SUM_H
	MOV	A,CODE_SUM_H
	JNZ	NO_CARRY			
	INC	CODE_SUM_C	
NO_CARRY:	
	MOV	A,R0
	RET

INIT_COUNT:
	MOV 	COUNT_L,#0
	MOV 	COUNT_H,#0
	MOV 	COUNT_C,#0FEh
	RET
START_COUNT:
	INC	COUNT_L 
	MOV	A,COUNT_L
	JNZ	NO_OVERFLOW
	INC	COUNT_H
	MOV	A,COUNT_H
	JNZ	NO_OVERFLOW
	INC	COUNT_C
	MOV	A,COUNT_C
	JNZ	NO_OVERFLOW
	JMP	start
NO_OVERFLOW:	
	RET
        END
